package com.pro_one.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pro_one.dao.MenuDao;
import com.pro_one.dao.RoleDao;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.pojo.Menu;
import com.pro_one.service.MenuService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private RoleDao roleDao;

    /**
     * 根据用户名递归查询，动态展示菜单
     *
     * @param username
     * @return
     */
    @Override
    public List getMenuByUsername(String username) {
        List<Menu> menuByUsername = menuDao.getMenuByUsername(username);
        List<Map> menuList = new ArrayList();
        for (Menu menu : menuByUsername) {
            if (menu.getParentMenuId() == null) {
                Map<String, Object> rootMenu = new HashMap<>();
                rootMenu.put("path", menu.getPath());
                rootMenu.put("title", menu.getName());
                rootMenu.put("icon", menu.getIcon());
                //调用递归
                List<Map> childMenu = getChildMenu(menu.getId());
                rootMenu.put("children", childMenu);
                menuList.add(rootMenu);
            }
        }
        return menuList;
    }


    public List<Map> getChildMenu(Integer id) {
        List<Map> childMenus = menuDao.findAssosiationById(id);
        if (childMenus == null || childMenus.size() == 0) {
            return childMenus;
        }
        for (Map childMenu : childMenus) {
            List<Map> menu = getChildMenu((Integer) childMenu.get("id"));
            if (menu == null || menu.size() == 0) {
                return childMenus;
            }
            childMenu.put("children", menu);
        }
        return childMenus;
    }
    @Override
    public PageResult findAllMenu(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Menu> menus=null;
        if (queryPageBean.getQueryString()==null||queryPageBean.getQueryString().isEmpty()) {
            menus = menuDao.findAllMenu();
        }else {
            menus=menuDao.findAllMenuByQueryString(queryPageBean.getQueryString());
        }
        if (menus==null||menus.isEmpty()) {

        }else {
            for (Menu menu : menus) {
                List<Menu> allByParentId = menuDao.findAllByParentId(menu.getId());
                if (allByParentId!=null&&allByParentId.size()>0) {
                    menu.setChildren(allByParentId);
                }
            }
        }

        return new PageResult(menus.getTotal(),menus.getResult());
    }

    @Override
    public void addMenu(Menu menu) {
        Integer level = menu.getLevel();
        Integer max=0;

        if (level==null||level==1) {
            menu.setLevel(1);
            List<Integer> pathByLevel1 = menuDao.findPathByLevel1();
            for (Integer integer : pathByLevel1) {
                max=max>integer?max:integer;
            }
            menu.setPath(max+1+"");
        }else {
            Integer maxPriority = menuDao.findPathByParentId(menu.getParentMenuId());
            if (maxPriority==null) {
                maxPriority=0;
            }
            int i = maxPriority + 1;
            menu.setPriority(i);
            menu.setPath("/"+menu.getParentMenuId()+"-"+ i);
        }
        menuDao.addMenu(menu);
        Map map = new HashMap();
        map.put("roleId",1);
        map.put("menuId",menu.getId());
        roleDao.addRoleIdAndMenuId(map);


    }

    @Override
    public void deleteMenu(Integer id) {
        menuDao.deleteRoleIdAndMenuIdByMenuId(id);
        menuDao.deleteMenu(id);
    }

    @Override
    public void updateMenu(Menu menu) {
        deleteMenu(menu.getId());
        addMenu(menu);
    }

    @Override
    public List<Menu> findMenusByRoleId(Integer id) {
        return null;
    }

    @Override
    public List<Menu> findAllByParentId(Integer id) {
        List<Menu> allByParentId = menuDao.findAllByParentId(id);
        return allByParentId;
    }

    /**
     * 新增的时候，认父菜单
     * @return
     */
    @Override
    public List<Map> findParentMenuId() {
        List<Map> parents = menuDao.findParentMenuId();
        return parents;
    }

    @Override
    public PageResult findAllMenuByRoleId(Integer page,Integer size,Integer roleId) {
        PageHelper.startPage(page,size);
        Page<Map> allMenuByRoleId = menuDao.findAllMenuByRoleId(roleId);
        PageResult pageResult = new PageResult(allMenuByRoleId.getTotal(), allMenuByRoleId.getResult());
        return pageResult;
    }

    @Override
    public Integer[] findMenuIdsByRoleId(Integer roleId) {
        return  menuDao.findMenuIdsByRoleId(roleId);
    }
}
